package com.catersystem.zmy.catersystem.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.catersystem.zmy.catersystem.R;
import com.catersystem.zmy.catersystem.adapter.BillsLatelyCompleteAdapter;
import com.catersystem.zmy.catersystem.adapter.LatelyCompleteAdapter;
import com.catersystem.zmy.catersystem.base.BaseActivity;
import com.catersystem.zmy.catersystem.bean.BehindBillsBean;
import com.catersystem.zmy.catersystem.bean.DishTypeBean;
import com.catersystem.zmy.catersystem.bean.LatelyCompleteAllDishesBean;
import com.catersystem.zmy.catersystem.bean.LatelyCompleteBean;
import com.catersystem.zmy.catersystem.bean.LatelyCompleteDishesBean;
import com.catersystem.zmy.catersystem.finals.FinalUrl;
import com.catersystem.zmy.catersystem.httputils.GetDataUtils;
import com.catersystem.zmy.catersystem.httputils.GsonUtil;
import com.catersystem.zmy.catersystem.httputils.HttpUtils;
import com.catersystem.zmy.catersystem.utils.AudioUtils;
import com.catersystem.zmy.catersystem.utils.DateUtil;
import com.catersystem.zmy.catersystem.view.NewRecycleView;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/10/30 0030.
 */

public class LatelyCompleteActivity extends BaseActivity implements BillsLatelyCompleteAdapter.IGetPositionListener {
    private static final String TAG = "LatelyCompleteActivity";
    @BindView(R.id.iv_cancel)
    ImageView mIvCancel;//取消
    @BindView(R.id.tv_return_home)
    TextView mTvReturnHome;//返回主页
    @BindView(R.id.tv_switch_show)
    TextView mTvSwitchShow;//切换显示样式
    @BindView(R.id.rv_dishes)
    RecyclerView mRvDishes;
    @BindView(R.id.tv_complete_time)
    TextView mTvCompleteTime;//完成时间
    @BindView(R.id.tv_call_out)
    TextView mTvCallOut;//呼叫
    @BindView(R.id.tv_no_complete)
    TextView mTvNoComplete;//还未完成


    private List<LatelyCompleteDishesBean> mLatelyCompleteDishesBeans; //最近完成-菜品模拟数据
    private List<LatelyCompleteAllDishesBean> mLatelyCompleteAllDishesBeans; //模拟后厨单据数据
    private LatelyCompleteAdapter latelyCompleteAdapter;
    private int mPosition = -1;
    private BillsLatelyCompleteAdapter billsLatelyCompleteAdapter;
    private int id = 1;//默认显示按菜分类

    public LatelyCompleteActivity() {
        super(R.layout.activity_lately_complete);
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        id = extras.getInt("id");
    }

    @Override
    protected void initView() {
        AudioUtils.getInstance().init(this);
        setLatelyCompleteAdapter();
    }

    /**
     * 最近完成适配器
     */
    private void setLatelyCompleteAdapter() {
        //设置布局样式

        if (id == 1) { //后厨
            if (mLatelyCompleteDishesBeans == null) {
                mLatelyCompleteDishesBeans = new ArrayList<>();
            }
            mTvNoComplete.setVisibility(View.VISIBLE);
            getLatelyCompleteData();
            mTvSwitchShow.setText("按单显示");
            mRvDishes.setLayoutManager(new GridLayoutManager(this, 5));
            latelyCompleteAdapter = new LatelyCompleteAdapter(mLatelyCompleteDishesBeans);
            mRvDishes.setAdapter(latelyCompleteAdapter);
            latelyCompleteAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    mPosition = position;
                    latelyCompleteAdapter.setPosition(position);
                    mTvCompleteTime.setVisibility(View.VISIBLE);
                    mTvCompleteTime.setText("完成时间：" + ((LatelyCompleteDishesBean) adapter.getData
                            ().get(position)).getTimestamp());
                    mTvCallOut.setClickable(true);
                }
            });
        } else { //单据
            if (mLatelyCompleteAllDishesBeans == null) {
                mLatelyCompleteAllDishesBeans = new ArrayList<>();
            }
            mTvNoComplete.setVisibility(View.GONE);
            getLatelyCompleteAllData();
            mTvSwitchShow.setText("按菜显示");
            mRvDishes.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.HORIZONTAL));
            billsLatelyCompleteAdapter = new BillsLatelyCompleteAdapter(this, mLatelyCompleteAllDishesBeans);
            mRvDishes.setAdapter(billsLatelyCompleteAdapter);
            billsLatelyCompleteAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    mPosition = position;
                    billsLatelyCompleteAdapter.setPosition(position);
                }
            });
        }
    }

    /**
     * 按菜分类 最近完成的菜品信息
     **/
    private void getLatelyCompleteData() {
        Map<String, String> data = new HashMap<>();
        data.put("orgcode", FinalUrl.getOrgCode());
        data.put("appguid", "e807cd996be775b69f3510fd4b742564");
        data.put("tablename", "data_tabledetail");
        data.put("field", "id,item_name,spunit,amount,taste,status, timestamp");
        //状态为2,3并且时间大于每天的8点
        data.put("where", "status in(2,3) and UNIX_TIMESTAMP(`opertime`) > " + DateUtil.getTodayTime());
        GetDataUtils.getInstance().getDishData(data, this, true, new HttpUtils.IHttpResult() {
            @Override
            public void onSuccess(String result) {
                mLatelyCompleteDishesBeans.clear();
                mLatelyCompleteDishesBeans.addAll(GsonUtil.jsonToList(result, LatelyCompleteDishesBean.class));
                latelyCompleteAdapter.notifyDataSetChanged();
            }
        });
    }

    /**
     * 按单分类获取最近完成的整单信息
     **/
    private void getLatelyCompleteAllData() {
        Map<String, String> data = new HashMap<>();
        data.put("orgcode", FinalUrl.getOrgCode());
        data.put("timestamp", DateUtil.getTodayTime() + "");
        data.put("status", "2");
        GetDataUtils.getInstance().getAllDishData(data, this, true, new HttpUtils.IHttpResult() {
            @Override
            public void onSuccess(String result) {
                mLatelyCompleteAllDishesBeans.clear();
                mLatelyCompleteAllDishesBeans.addAll(GsonUtil.jsonToList(result, LatelyCompleteAllDishesBean.class));
                billsLatelyCompleteAdapter.notifyDataSetChanged();
            }
        });
    }


    /**
     * 最近完成--点击事件
     */
    @OnClick({R.id.tv_switch_show, R.id.tv_return_home, R.id.iv_cancel, R.id.tv_no_complete, R.id.tv_call_out})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_cancel:
            case R.id.tv_return_home:
                Intent intent = new Intent();
                intent.putExtra("id", id);
                setResult(RESULT_OK, intent);
                finish();
                break;
            case R.id.tv_switch_show:
                if (id == 1) {
                    id = 2;
                } else {
                    id = 1;
                }
                mPosition = -1;
                mTvCompleteTime.setVisibility(View.GONE);
                setLatelyCompleteAdapter();
                break;
            case R.id.tv_no_complete: //还未完成
                if (mPosition != -1) {
                    Map<String, String> data = new HashMap<>();
                    data.put("orgcode", FinalUrl.getOrgCode());
                    data.put("detailid", ((LatelyCompleteDishesBean) latelyCompleteAdapter.getData().get(mPosition))
                            .getId() + "");
                    GetDataUtils.getInstance().refreshCookingStatus(data, this, new HttpUtils
                            .IHttpResult() {
                        @Override
                        public void onSuccess(String result) {
                            latelyCompleteAdapter.getData().remove(mPosition);
                            latelyCompleteAdapter.notifyItemRemoved(mPosition);
                            mPosition = -1;
                        }
                    });
                    break;
                }
                break;
            case R.id.tv_call_out: //呼叫
                if (mPosition == -1) {
                    showShortToast("请选择需要呼叫的菜品！");
                    return;
                }
                if (id == 1) {
                    AudioUtils.getInstance().speakText(((LatelyCompleteDishesBean) latelyCompleteAdapter.getData().get
                            (mPosition)).getId() + "号请到后厨取餐！");
                    showShortToast(((LatelyCompleteDishesBean) latelyCompleteAdapter.getData().get(mPosition))
                            .getId() + "号请到后厨取餐！");
                } else {
                    AudioUtils.getInstance().speakText(((LatelyCompleteAllDishesBean) billsLatelyCompleteAdapter
                            .getData().get(mPosition)).getId() + "号请到后厨取餐！");
                }
                break;
        }
    }


    @Override
    public void getPosition(int position) {
        mPosition = position;
        mTvCompleteTime.setVisibility(View.VISIBLE);
        mTvCompleteTime.setText("完成时间：" + ((LatelyCompleteAllDishesBean) billsLatelyCompleteAdapter
                .getData().get(position)).getTimestamp());
        mTvCallOut.setClickable(true);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AudioUtils.getInstance().onDestroy();
    }
}
